from .models import *
from rest_framework import serializers


# 用户
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserModel
        fields = ['id', 'nickname', 'avatar', 'email', 'introduce']


# 使用嵌套序列化器
class UserPostSerializer(serializers.ModelSerializer):
    avatar = serializers.SerializerMethodField()
    class Meta:
        model = UserModel
        fields = ['id', 'nickname', 'avatar', 'introduce']

    def get_avatar(self, obj):
        return obj.avatar.name  # 只返回相对路径
# 帖子评论序列化器
class CommentSerializer(serializers.ModelSerializer):
    user = UserPostSerializer()

    class Meta:
        model = CommentModel
        fields = ['id', 'content', 'created_at', 'user']

# 帖子
class PostSerializer(serializers.ModelSerializer):
    user = UserPostSerializer()

    class Meta:
        model = PostModel
        fields = ['id', 'title', 'content', 'category', 'created_at', 'updated_at', 'comment_count', 'like_count', 'favorite_count', 'user']


class LikeSerializer(serializers.ModelSerializer):
    post = PostSerializer()
    class Meta:
        model = LikeModel
        fields = ['id', 'user', 'post']


class FavoriteSerializer(serializers.ModelSerializer):
    post = PostSerializer()
    class Meta:
        model = LikeModel
        fields = ['id', 'user', 'post']

